<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="预约时间" prop="tdate" >
      <el-input v-model="dataForm.tdate=getDay(1)" placeholder="预约时间" readonly="true"></el-input>

    </el-form-item>
    <el-form-item label="菜品名称" prop="dishName">
      <el-input v-model="dataForm.dishName" placeholder="菜品名称"></el-input>
    </el-form-item>
    <el-form-item label="数量" prop="num">
      <el-input v-model="dataForm.num" placeholder="数量"></el-input>
    </el-form-item>

      <el-form-item label="身份证" prop="num">
<!--        <el-input v-model="dataForm.idCard" placeholder="身份证"></el-input>-->
<!--        <el-input v-model="dataForm.idCard" placeholder="身份证" @input="onInput('idCard', $event)"></el-input>-->
        <el-input v-model="dataForm.idCard" placeholder="身份证" onkeyup="this.value=this.value.replace(/^[1-9]\d{5}(19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
,'');" maxlength="18" show-word-limit clearable />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          tdate: '',
          dishName: '',
          num: '',
          idCard: ''
        },

        dataRule: {
          tdate: [
            { required: true, message: '预约时间不能为空', trigger: 'blur' }
          ],
          dishName: [
            { required: true, message: '菜品名称不能为空', trigger: 'blur' }
          ],
          num: [
            { required: true, message: '数量不能为空', trigger: 'blur' }
          ],
          idCard: [
            { required: true, message: '身份证不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      // 获取当前时间，day为number，getDay(-1):昨天的日期;getDay(0):今天的日期;getDay(1):明天的日期;【以此类推】
      getDay (day) {
        var today = new Date();
        var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
        today.setTime(targetday_milliseconds); //注意，这行是关键代码

        var tYear = today.getFullYear();
        var tMonth = today.getMonth();
        var tDate = today.getDate();
        tMonth = this.doHandleMonth(tMonth + 1);
        tDate = this.doHandleMonth(tDate);
        return tYear + "-" + tMonth + "-" + tDate;
      },
      doHandleMonth(month) {
        var m = month;
        if (month.toString().length == 1) {
          m = "0" + month;
        }
        return m;
      },

      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/menu/tomorrow/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.tdate = data.tomorrow.tdate
                this.dataForm.dishName = data.tomorrow.dishName
                this.dataForm.num = data.tomorrow.num
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              // url: this.$http.adornUrl(`/menu/tomorrow/${!this.dataForm.id ? 'save' : 'update'}`),
              url: `http://localhost:8081/menu/tomorrow/${!this.dataForm.id ? 'save' : 'update'}`,
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'tdate': this.dataForm.tdate,
                'dishName': this.dataForm.dishName,
                'num': this.dataForm.num,
                'idCard': this.dataForm.idCard
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    },

  }
</script>
